package com.hnou.boxuegu.view;

import android.app.Activity;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;

import com.hnou.boxuegu.R;
import com.hnou.boxuegu.adapter.ExercisesAdapter;
import com.hnou.boxuegu.bean.ExercisesBean;
import com.hnou.boxuegu.utils.Constant;
import com.hnou.boxuegu.utils.JsonParse;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class ExercisesView {
    private static final String TAG = "ExercisesView";
    private RecyclerView rv_list;
    private ExercisesAdapter adapter;
    private Activity mContext;
    private LayoutInflater mInflater;
    private View mCurrentView;
    private List<ExercisesBean> ebl;
    public ExercisesView(Activity context){
        mContext=context;
        mInflater=LayoutInflater.from(mContext);
    }
    private MHandler mHandler;
    public static final int MSG_EXERCISES_OK = 1;
    private void initView(){
        ebl=new ArrayList<>();
        mCurrentView=mInflater.inflate(R.layout.main_view_exercises,null);
        rv_list=mCurrentView.findViewById(R.id.rv_list);
        rv_list.setLayoutManager(new LinearLayoutManager(mContext));
        adapter=new ExercisesAdapter(mContext);
        rv_list.setAdapter(adapter);
        mHandler=new MHandler();
        getExercisesData();
    }
    private void getExercisesData(){
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder().url(Constant.WEB_SITE + Constant.REQUEST_EXERCISES_URL).build();
        Call call=okHttpClient.newCall(request);
        call.enqueue(new Callback(){

            @Override
            public void onFailure(Call call, IOException e) {
                Log.e(TAG, "onFailure: ",e );
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String res=response.body().string();
                Log.e(TAG, "onResponse: "+res );
                Message msg=new Message();
                msg.obj=res;
                msg.what= MSG_EXERCISES_OK;
                mHandler.sendMessage(msg);
            }
        });
    }
    class MHandler extends Handler{
        @Override
        public void dispatchMessage(Message msg){
            super.dispatchMessage(msg);
            switch (msg.what){
                case MSG_EXERCISES_OK:
                    if(msg.obj != null){
                        String vlResult = (String) msg.obj;
                        if(ebl!=null)ebl.clear();
                        ebl= JsonParse.getInstance().getExercisesList(vlResult);
                        adapter.setData(ebl);
                    }
                    break;
            }
        }
    }
    public View getView(){
        if(mCurrentView == null){
            initView();
        }
        return mCurrentView;
    }
    public void showView(){
        if(mCurrentView == null){
            initView();
        }
        mCurrentView.setVisibility(View.VISIBLE);
    }
}
